package com.example.administrator.appstpremvpdemo.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.example.administrator.appstpremvpdemo.manager.LocalActivityManager;

/**
 * Created by Administrator on 2017/12/11.
 */

public class BaseActivity extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LocalActivityManager.getInctance().addActivity(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalActivityManager.getInctance().removeActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        LocalActivityManager.getInctance().clickBack(this);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        //内存不足的时候，应该执行清除无关内存的占用，确保当前界面交互的流畅性
        //这个方法可以在LocalActivityManager中定义
    }

    /**
     * 跳转activity,不携带参数
     *
     * @param activity
     * @param clazz    要跳转的类
     * @param isfinish 是否finish当前activity
     */
    public static void goTo(Activity activity, Class<?> clazz, boolean isfinish) {
        activity.startActivity(new Intent(activity, clazz));
        if (isfinish) {
            activity.finish();
        }
    }

    /**
     * 跳转activity,携带参数
     *
     * @param activity
     * @param bundle   参数可以设置在bundle中
     * @param clazz    要跳转的类
     * @param isfinish 是否finish当前activity
     */
    public static void goTo(Activity activity, Bundle bundle, Class<?> clazz, boolean isfinish) {
        Intent intent = new Intent(activity, clazz);
        intent.putExtras(bundle);
        activity.startActivity(new Intent(activity, clazz));
        if (isfinish) {
            activity.finish();
        }
    }
}
